package com.example.easybuy.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.easybuy.pojo.Collectionproducts;
import com.example.easybuy.pojo.Products;
import com.example.easybuy.pojo.User;
import com.example.easybuy.service.CollectionproductsService;
import org.apache.log4j.Logger;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@CrossOrigin
public class CollectionproductsController {

    private Logger logger=Logger.getLogger(getClass());

    @Resource
    private CollectionproductsService collectionproductsService;

    @Resource
    private RedisTemplate redisTemplate;

    /*
    添加收藏商品
     */
    @RequestMapping("addConllectionProducts")
    @ResponseBody
    public String addConllectionProducts(String productsId){
        logger.debug("CollectionproductsController-addConllectionProducts()-productsId:"+productsId);
        //查询收藏的商品信息
        Products products=collectionproductsService.getProductsById(productsId);

        //拿到存放在redis里面的登录用户
        String user=(String)redisTemplate.opsForValue().get("user");
        //把user Json字符串转换为user对象
        User userObj = JSONObject.parseObject(user, User.class);
        logger.debug("CollectionproductsController-addConllectionProducts()-userObj:"+userObj);
        //用户账号id
        int userId=userObj.getId();

        //收藏的商品
        Collectionproducts collectionproducts=new Collectionproducts();
        collectionproducts.setName(products.getName());
        collectionproducts.setDescribe(products.getDescribe());
        collectionproducts.setPrice(products.getPrice());
        collectionproducts.setFileName(products.getFileName());
        collectionproducts.setUserId(userId);
        int proId=Integer.valueOf(products.getId());
        collectionproducts.setProductsId(proId);
        collectionproducts.setStock(products.getStock());
        logger.debug("CollectionproductsController-addConllectionProducts()-collectionproducts:"+collectionproducts);
        String success=collectionproductsService.addConllectionProducts(collectionproducts);
        logger.debug("CollectionproductsController-addConllectionProducts()-success:"+success);
        return success;
    }

    /*
    查询当前商品在不在当前用户的收藏夹里
     */
    @RequestMapping("getProductsIsExist")
    @ResponseBody
    public Collectionproducts getProductsIsExist(String productsId){
        logger.debug("CollectionproductsController-getProductsIsExist()-productsId:"+productsId);
        //拿到存放在redis里面的登录用户
        String user=(String)redisTemplate.opsForValue().get("user");
        //把user Json字符串转换为user对象
        User userObj = JSONObject.parseObject(user, User.class);
        logger.debug("CollectionproductsController-getProductsIsExist()-userObj:"+userObj);
        //用户账号id
        int userId=userObj.getId();

        Collectionproducts collectionproducts=collectionproductsService.getProductsIsExist(productsId,userId);
        logger.debug("CollectionproductsController-getProductsIsExist()-collectionproducts:"+collectionproducts);
        return collectionproducts;
    }

    /*
    收藏了几个商品，最多只能收藏6个
     */
    @RequestMapping("getCollUserIdCount")
    @ResponseBody
    public int getCollUserIdCount(){
        logger.debug("CollectionproductsController-getCollUserIdCount()...start...");
        //拿到存放在redis里面的登录用户
        String user=(String)redisTemplate.opsForValue().get("user");
        //把user Json字符串转换为user对象
        User userObj = JSONObject.parseObject(user, User.class);
        logger.debug("CollectionproductsController-getCollUserIdCount()-userObj:"+userObj);
        //用户账号id
        int userId=userObj.getId();

        int colleCount=collectionproductsService.getCollUserIdCount(userId);
        logger.debug("CollectionproductsController-getCollUserIdCount()-colleCount:"+colleCount);
        return colleCount;
    }

    /*
    查询当前用户所有的收藏商品
     */
    @RequestMapping("getColleAll")
    @ResponseBody
    public List<Collectionproducts> getColleAll(){
        logger.debug("CollectionproductsController-getColleAll()...start...");
        //拿到存放在redis里面的登录用户
        String user=(String)redisTemplate.opsForValue().get("user");
        //把user Json字符串转换为user对象
        User userObj = JSONObject.parseObject(user, User.class);
        logger.debug("CollectionproductsController-getColleAll()-userObj:"+userObj);
        //用户账号id
        int userId=userObj.getId();

        List<Collectionproducts> colleList=collectionproductsService.getColleAll(userId);
        logger.debug("CollectionproductsController-getColleAll()-colleList:"+colleList);
        return colleList;
    }

    /*
    通过商品id和用户id删除收藏商品
     */
    @RequestMapping("deleteColleproduct")
    @ResponseBody
    public String deleteColleproduct(String productsId){
        logger.debug("CollectionproductsController-deleteColleproduct()-productsId:"+productsId);
        //拿到存放在redis里面的登录用户
        String user=(String)redisTemplate.opsForValue().get("user");
        //把user Json字符串转换为user对象
        User userObj = JSONObject.parseObject(user, User.class);
        logger.debug("CollectionproductsController-deleteColleproduct()-userObj:"+userObj);
        //用户账号id
        int userId=userObj.getId();

        String success=collectionproductsService.deleteColleproduct(userId,productsId);
        logger.debug("CollectionproductsController-deleteColleproduct()-success:"+success);
        return success;
    }

    /*
    清空所有收藏商品
     */
    @RequestMapping("clearAllColle")
    @ResponseBody
    public String clearAllColle(){
        logger.debug("CollectionproductsController-clearAllColle()...start...");
        //拿到存放在redis里面的登录用户
        String user=(String)redisTemplate.opsForValue().get("user");
        //把user Json字符串转换为user对象
        User userObj = JSONObject.parseObject(user, User.class);
        logger.debug("CollectionproductsController-deleteColleproduct()-userObj:"+userObj);
        //用户账号id
        int userId=userObj.getId();

        String success=collectionproductsService.clearAllColle(userId);
        logger.debug("CollectionproductsController-clearAllColle()-success:"+success);
        return success;
    }

}
